package tv.acgn.advertisement.zuul.filter;

import java.time.Duration;
import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

/**
 * @program: advertisement
 * @description: 网关-请求许可日志-后置过滤器
 * @author: junG
 * @create: 2019-04-21 23:43
 **/

@Slf4j
public class AccessLogPostRequestFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER-1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest httpServletRequest = requestContext.getRequest();
        String uri=httpServletRequest.getRequestURI();
        String clientIP = httpServletRequest.getRemoteAddr();

        LocalDateTime enterTime = (LocalDateTime)requestContext.get("reqStartTime");
        LocalDateTime now=LocalDateTime.now();
        Long seconds=Duration.between(enterTime,LocalDateTime.now()).getSeconds();

        log.trace("请求{}目标{}在{}转出网关 记录通过耗时{}", clientIP, uri, now, seconds);
        return null;
    }
}
